const express = require("express");
const http = require("http");
const webSocket = require("ws");
const app = express();
const server = http.createServer(app);
const wss = new webSocket.Server({ server });
//webSocket服务器
wss.on("connection", function (ws) {
  console.log("连接成功");
  ws.on("message", function (msg) {
    //处理收到的信息
    const buffer = Buffer.from(msg)
    const text = buffer.toString('utf-8')
    console.log(text)
  });
  ws.on("close", function () {
    console.log("连接关闭");
  });
  ws.on("error", function () {
    console.log("连接错误");
  });
});
app.get("/", function (req, res) {
  res.send("hello world");
});
server.listen(3000, function () {
  console.log("服务器运行在 http://localhost:3000");
});
